/*
=======================================================================

Add Players to Menu

=======================================================================
*/
#include "ui_local.h"
#include "ui_BOTS.h"

BOTS_ItemAction_e globalItemAction_ev;
char globalItemList[100][100];
char mapvote_info[MAX_INFO_STRING] = {0};

#define ART_BACK0			"menu/art/back_0"
#define ART_BACK1			"menu/art/back_1"	
#define ART_FIGHT0			"menu/art/accept_0"
#define ART_FIGHT1			"menu/art/accept_1"
#define ART_BACKGROUND		"menu/art/addbotframe"
#define ART_ARROWS			"menu/art/arrows_vert_0"
#define ART_ARROWUP			"menu/art/arrows_vert_top"
#define ART_ARROWDOWN		"menu/art/arrows_vert_bot"

#define BOTS_ADDITEMS_EXECUTE	1
#define BOTS_ADDITEMS_UP		2
#define BOTS_ADDITEMS_DOWN		3
#define BOTS_ADDITEMS_PLAYERID0	4
#define BOTS_ADDITEMS_PLAYERID1	5
#define BOTS_ADDITEMS_PLAYERID2	6
#define BOTS_ADDITEMS_PLAYERID3	7
#define BOTS_ADDITEMS_PLAYERID4	8
#define BOTS_ADDITEMS_PLAYERID5	9
#define BOTS_ADDITEMS_PLAYERID6	10

typedef struct 
{
	menuframework_s	menu;
	menubitmap_s	frame;
	menubitmap_s	arrows;
	menubitmap_s	up;
	menubitmap_s	down;
	menubitmap_s	execute;
	menubitmap_s	back;
	menutext_s		title;
	menutext_s		item[7];			// actual names we see

	int				numItems;			// total number of players passed to menu
	int				baseItemNum;		// item number at the top of the menu
	int				selectedItemNum;	// current item number
	char			item_names[7][32];	// variable item names that are assigned to 'item'
} addItemsToMenu_t;

static addItemsToMenu_t	s_addItemsToMenu;

/*
=================
BOTS_AddItemsToMenu_Event
=================
*/
void BOTS_AddItemsToMenu_Event( void* ptr, int event ) 
{
	int  numPlayers;
	char info[MAX_INFO_STRING];

	if( event != QM_ACTIVATED )
		return;

	trap_GetConfigString(CS_SERVERINFO, info, sizeof(info));
	numPlayers = atoi(Info_ValueForKey( info, "sv_maxclients"));

	// any event will kill the menu
	UI_PopMenu();

	switch( ((menucommon_s*)ptr)->id ) 
	{
	case BOTS_CANCEL:
		break;
	case BOTS_ADDITEMS_EXECUTE:
		{
			char action[100];
			int  position;
			int	 clientNum;

			switch (globalItemAction_ev)
			{
			case BOTS_ITEM_MENU_VOTE_E:
				strcpy(action, "UI_to_server_vote");
				break;
			case BOTS_ITEM_MENU_MUTE_E:
				strcpy(action, "UI_to_server_mute");
				break;
			case BOTS_ITEM_MENU_UMUTE_E:
				strcpy(action, "UI_to_server_unmute");
				break;
			case BOTS_ITEM_MENU_PROMOTE_E:
				strcpy(action, "UI_to_server_promote");
				break;
			case BOTS_ITEM_MENU_DEMOTE_E:
				strcpy(action, "UI_to_server_demote");
				break;
			case BOTS_ITEM_MENU_VIDEO_E:
				strcpy(action, "UI_to_server_video");
				break;
			case BOTS_ITEM_MENU_HEAL_E:
				strcpy(action, "UI_to_server_heal");
				break;			     
			case BOTS_ITEM_MENU_MAPVOTE_E:
				strcpy(action, "UI_to_server_mapvote");
				trap_Cmd_ExecuteText( EXEC_APPEND, va("%s %s\n", action, s_addItemsToMenu.item[s_addItemsToMenu.selectedItemNum].string));
				return;
			}

			if (globalItemAction_ev == BOTS_ITEM_MENU_PROMOTE_E ||
				globalItemAction_ev == BOTS_ITEM_MENU_DEMOTE_E)
			{
				position = strlen(s_addItemsToMenu.item[s_addItemsToMenu.selectedItemNum].string) - 8;
				s_addItemsToMenu.item[s_addItemsToMenu.selectedItemNum].string[position] = 0;
			}

			for (clientNum = 0; clientNum <= numPlayers; clientNum++)
			{
				char netname[30];
				char cs_player_info[MAX_INFO_STRING];

				trap_GetConfigString( CS_PLAYERS + clientNum, cs_player_info, MAX_INFO_STRING );
				strcpy(netname, Info_ValueForKey( cs_player_info, "n"));
				Q_CleanStr(netname);
				if (Q_stricmp(s_addItemsToMenu.item[s_addItemsToMenu.selectedItemNum].string, netname) != 0)
					continue;

				trap_Cmd_ExecuteText( EXEC_APPEND, va("%s %i\n", action, clientNum));
				break;
			}

			/*
			if (s_addItemsToMenu.selectedItemNum >= 0 &&
				s_addItemsToMenu.item[s_addItemsToMenu.selectedItemNum].string)
				trap_Cmd_ExecuteText( EXEC_APPEND, va("%s %s\n", action, 
									  s_addItemsToMenu.item[s_addItemsToMenu.selectedItemNum].string));
									  */
		}
		break;

	// oops - not a valid choice
	default:
		Com_Printf("%i is not a valid choice\n", ((menucommon_s*)ptr)->id);
		break;
	} // switch 
}

/*
=================
BOTS_AddItemsToMenu_SelectEvent
=================
*/
void BOTS_AddItemsToMenu_SelectEvent( void* ptr, int event )
{
	if (event != QM_ACTIVATED) 
		return;

	// unhighlite old selection
	s_addItemsToMenu.item[s_addItemsToMenu.selectedItemNum].color = color_bots_menu;

	// highlite new selection
	s_addItemsToMenu.selectedItemNum = ((menucommon_s*)ptr)->id - BOTS_ADDITEMS_PLAYERID0;
	s_addItemsToMenu.item[s_addItemsToMenu.selectedItemNum].color = color_white;
}

/*
=================
BOTS_AddItemsToMenu_SetPlayerNames
Constructs the 7 (or less) item names to be displayed
=================
*/
void BOTS_AddItemsToMenu_SetPlayerNames()
{
	int n;

	for (n = 0; n < 7; n++)
		strcpy(s_addItemsToMenu.item_names[n], globalItemList[n + s_addItemsToMenu.baseItemNum]);
}

/*
=================
BOTS_AddItemsToMenu_UpEvent
=================
*/
void BOTS_AddItemsToMenu_UpEvent( void* ptr, int event) {
	if (event != QM_ACTIVATED) {
		return;
	}

	if( s_addItemsToMenu.baseItemNum > 0 ) {
		s_addItemsToMenu.baseItemNum--;
		BOTS_AddItemsToMenu_SetPlayerNames();
	}
}


/*
=================
BOTS_AddItemsToMenu_DownEvent
=================
*/
void BOTS_AddItemsToMenu_DownEvent( void* ptr, int event ) 
{
	if (event != QM_ACTIVATED)
		return;

	if( s_addItemsToMenu.baseItemNum + 7 < s_addItemsToMenu.numItems ) {
		s_addItemsToMenu.baseItemNum++;
		BOTS_AddItemsToMenu_SetPlayerNames();
	}
}

/*
=================
BOTS_AddItemsToMenu_Cache
=================
*/
void BOTS_AddItemsToMenu_Cache( void ) {
	trap_R_RegisterShaderNoMip( ART_BACK0 );
	trap_R_RegisterShaderNoMip( ART_BACK1 );
	trap_R_RegisterShaderNoMip( ART_FIGHT0 );
	trap_R_RegisterShaderNoMip( ART_FIGHT1 );
	//trap_R_RegisterShaderNoMip( ART_BACKGROUND );
	trap_R_RegisterShaderNoMip( ART_ARROWS );
	trap_R_RegisterShaderNoMip( ART_ARROWUP );
	trap_R_RegisterShaderNoMip( ART_ARROWDOWN );
}

/*
=================
BOTS_AddItemsToMenu_Init
=================
*/
void BOTS_AddItemsToMenu_Init( void ) {
	int		n;
	int		y;
	int		count;

	uiClientState_t	cs;
	char info[MAX_INFO_STRING];

	const char	*mapname;

	static char menuName[100];

	// setup images
	BOTS_AddItemsToMenu_Cache();

	memset( &s_addItemsToMenu, 0 ,sizeof(s_addItemsToMenu) );
	s_addItemsToMenu.menu.fullscreen = qfalse;
	s_addItemsToMenu.menu.wrapAround = qtrue;

	// get the config string and the data it holds
	trap_GetClientState( &cs );
	
	if (globalItemAction_ev == BOTS_ITEM_MENU_MAPVOTE_E) {
		if (!mapvote_info[0]) {
			trap_GetConfigString( CS_BOTS_MENU + cs.clientNum, info, MAX_INFO_STRING );
			s_addItemsToMenu.numItems = atoi(Info_ValueForKey( info, "size"));
		}
		else
			Q_strncpyz(info, mapvote_info, sizeof(mapvote_info));
	}
	else {
		trap_GetConfigString( CS_BOTS_MENU + cs.clientNum, info, MAX_INFO_STRING );
		s_addItemsToMenu.numItems = atoi(Info_ValueForKey( info, "size"));
	}
	
	count = s_addItemsToMenu.numItems < 7 ? s_addItemsToMenu.numItems : 7;

	if (globalItemAction_ev == BOTS_ITEM_MENU_MAPVOTE_E) {
		// Map voting is a bit different
		for (n = 0; n < s_addItemsToMenu.numItems; n++) {
			mapname = Info_ValueForKey( info, va("map%i", n));
			strcpy(globalItemList[n], mapname);
			Q_CleanStr(globalItemList[n]);
		}
	}
	else {
		// assign the items to the global array - cause I'm too damn lazy to make
		// a link list
		for (n = 0; n < s_addItemsToMenu.numItems; n++) 
		{
			int clientNum;
			int level;
			int classtype;
			char cs_player_info[MAX_INFO_STRING];
			char bots_player_info[MAX_INFO_STRING];
			char* classname = 0;

			clientNum = atoi( Info_ValueForKey( info, va("name%i", n)));
			trap_GetConfigString( CS_PLAYERS + clientNum, cs_player_info, MAX_INFO_STRING );
			strcpy(globalItemList[n], Info_ValueForKey( cs_player_info, "n"));
			Q_CleanStr(globalItemList[n]);

			if (globalItemAction_ev == BOTS_ITEM_MENU_PROMOTE_E ||
				globalItemAction_ev == BOTS_ITEM_MENU_DEMOTE_E)
			{
				trap_GetConfigString( CS_BOTS_CLIENT + clientNum, bots_player_info, MAX_INFO_STRING );
				level = atoi (Info_ValueForKey( bots_player_info, "lev"));
				classtype = atoi (Info_ValueForKey( bots_player_info, "c"));

				switch (classtype)
				{
				case BOTS_BODYGUARD:
					classname = "BG :";
					break;
				case BOTS_SNIPER:
					classname = "SNP:";
					break;
				case BOTS_SOLDIER:
					classname = "SOL:";
					break;
				case BOTS_BERZERKER:
					classname = "ZRK:";
					break;
				case BOTS_INFILTRATOR:
					classname = "INF:";
					break;
				case BOTS_KAMIKAZEE:
					classname = "KAM:";
					break;
				case BOTS_NURSE:
					classname = "NRS:";
					break;
				case BOTS_SCIENTIST:
					classname = "SCI:";
					break;
				}

				Q_strcat(globalItemList[n],100, va(" - %s%i", classname, level));
			}
		}
	}

	// get the menu name
	switch (globalItemAction_ev)
	{
	case BOTS_ITEM_MENU_VOTE_E:
		strcpy(menuName, "VOTE MENU");
		break;
	case BOTS_ITEM_MENU_MUTE_E:
		strcpy(menuName, "MUTE MENU");
		break;
	case BOTS_ITEM_MENU_UMUTE_E:
		strcpy(menuName, "UMNUTE MENU");
		break;
	case BOTS_ITEM_MENU_PROMOTE_E:
		strcpy(menuName, "PROMOTE MENU");
		break;
	case BOTS_ITEM_MENU_DEMOTE_E:
		strcpy(menuName, "DEMOTE MENU");
		break;
	case BOTS_ITEM_MENU_VIDEO_E:
		strcpy(menuName, "TEAM CAMERA MENU");
		break;
	case BOTS_ITEM_MENU_HEAL_E:
		strcpy(menuName, "HEAL MENU");
		break;
	case BOTS_ITEM_MENU_MAPVOTE_E:
		strcpy(menuName, "MAP VOTE MENU");
		break;
	}
	s_addItemsToMenu.title.generic.type		= MTYPE_PTEXT;
	s_addItemsToMenu.title.generic.flags	= QMF_CENTER_JUSTIFY;
	s_addItemsToMenu.title.generic.x		= 320;
	s_addItemsToMenu.title.generic.y		= 16;
	s_addItemsToMenu.title.string			= menuName;
	s_addItemsToMenu.title.color			= color_bots_menu;
	s_addItemsToMenu.title.style			= UI_CENTER|UI_SMALLFONT;
/*
	s_addItemsToMenu.frame.generic.type		= MTYPE_BITMAP;
	s_addItemsToMenu.frame.generic.flags	= QMF_INACTIVE;
	s_addItemsToMenu.frame.generic.name		= ART_BACKGROUND;
	s_addItemsToMenu.frame.generic.x		= 320-233;//142;
	s_addItemsToMenu.frame.generic.y		= 240-166;//118;
	s_addItemsToMenu.frame.width			= 466;//359;
	s_addItemsToMenu.frame.height			= 332;//256;
*/
	s_addItemsToMenu.arrows.generic.type	= MTYPE_BITMAP;
	s_addItemsToMenu.arrows.generic.name	= ART_ARROWS;
	s_addItemsToMenu.arrows.generic.flags	= QMF_INACTIVE;
	s_addItemsToMenu.arrows.generic.x		= 200;
	s_addItemsToMenu.arrows.generic.y		= 128;
	s_addItemsToMenu.arrows.width  			= 64;
	s_addItemsToMenu.arrows.height  	    = 128;

	s_addItemsToMenu.up.generic.type	    = MTYPE_BITMAP;
	s_addItemsToMenu.up.generic.flags		= QMF_LEFT_JUSTIFY|QMF_PULSEIFFOCUS;
	s_addItemsToMenu.up.generic.x			= 200;
	s_addItemsToMenu.up.generic.y			= 128;
	s_addItemsToMenu.up.generic.id			= BOTS_ADDITEMS_UP;
	s_addItemsToMenu.up.generic.callback	= BOTS_AddItemsToMenu_UpEvent;
	s_addItemsToMenu.up.width  				= 64;
	s_addItemsToMenu.up.height  		    = 64;
	s_addItemsToMenu.up.focuspic			= ART_ARROWUP;

	s_addItemsToMenu.down.generic.type		= MTYPE_BITMAP;
	s_addItemsToMenu.down.generic.flags		= QMF_LEFT_JUSTIFY|QMF_PULSEIFFOCUS;
	s_addItemsToMenu.down.generic.x			= 200;
	s_addItemsToMenu.down.generic.y			= 128+64;
	s_addItemsToMenu.down.generic.id		= BOTS_ADDITEMS_DOWN;
	s_addItemsToMenu.down.generic.callback	= BOTS_AddItemsToMenu_DownEvent;
	s_addItemsToMenu.down.width  			= 64;
	s_addItemsToMenu.down.height  			= 64;
	s_addItemsToMenu.down.focuspic			= ART_ARROWDOWN;

	// list the first 'count' players passed
	for( n = 0, y = 120; n < count; n++, y += 20 ) 
	{
		s_addItemsToMenu.item[n].generic.type		= MTYPE_PTEXT;
		s_addItemsToMenu.item[n].generic.flags		= QMF_LEFT_JUSTIFY|QMF_PULSEIFFOCUS;
		s_addItemsToMenu.item[n].generic.x			= 320 - 56;
		s_addItemsToMenu.item[n].generic.y			= y;
		s_addItemsToMenu.item[n].generic.id			= BOTS_ADDITEMS_PLAYERID0 + n;
		s_addItemsToMenu.item[n].generic.callback	= BOTS_AddItemsToMenu_SelectEvent;
		s_addItemsToMenu.item[n].string				= s_addItemsToMenu.item_names[n];
		s_addItemsToMenu.item[n].color				= color_bots_menu;
		s_addItemsToMenu.item[n].style				= UI_LEFT|UI_SMALLFONT;
	}

	s_addItemsToMenu.execute.generic.type		= MTYPE_BITMAP;
	s_addItemsToMenu.execute.generic.name		= ART_FIGHT0;
	s_addItemsToMenu.execute.generic.flags		= QMF_LEFT_JUSTIFY|QMF_PULSEIFFOCUS;
	s_addItemsToMenu.execute.generic.id			= BOTS_ADDITEMS_EXECUTE;
	s_addItemsToMenu.execute.generic.callback	= BOTS_AddItemsToMenu_Event;
	s_addItemsToMenu.execute.generic.x			= 320+128-128;
	s_addItemsToMenu.execute.generic.y			= 256+128-64;
	s_addItemsToMenu.execute.width  			= 128;
	s_addItemsToMenu.execute.height  			= 64;
	s_addItemsToMenu.execute.focuspic			= ART_FIGHT1;

	s_addItemsToMenu.back.generic.type		= MTYPE_BITMAP;
	s_addItemsToMenu.back.generic.name		= ART_BACK0;
	s_addItemsToMenu.back.generic.flags		= QMF_LEFT_JUSTIFY|QMF_PULSEIFFOCUS;
	s_addItemsToMenu.back.generic.id		= BOTS_CANCEL;
	s_addItemsToMenu.back.generic.callback	= BOTS_AddItemsToMenu_Event;
	s_addItemsToMenu.back.generic.x			= 320-128;
	s_addItemsToMenu.back.generic.y			= 256+128-64;
	s_addItemsToMenu.back.width				= 128;
	s_addItemsToMenu.back.height			= 64;
	s_addItemsToMenu.back.focuspic			= ART_BACK1;

	s_addItemsToMenu.baseItemNum = 0;
	s_addItemsToMenu.selectedItemNum = 0;
	s_addItemsToMenu.item[0].color = color_white;

	BOTS_AddItemsToMenu_SetPlayerNames();

	//Menu_AddItem( &s_addItemsToMenu.menu, &s_addItemsToMenu.frame );
	Menu_AddItem( &s_addItemsToMenu.menu, &s_addItemsToMenu.title );
	Menu_AddItem( &s_addItemsToMenu.menu, &s_addItemsToMenu.arrows );
	Menu_AddItem( &s_addItemsToMenu.menu, &s_addItemsToMenu.up );
	Menu_AddItem( &s_addItemsToMenu.menu, &s_addItemsToMenu.down );
	for( n = 0; n < count; n++ ) 
		Menu_AddItem( &s_addItemsToMenu.menu, &s_addItemsToMenu.item[n] );
	Menu_AddItem( &s_addItemsToMenu.menu, &s_addItemsToMenu.execute );
	Menu_AddItem( &s_addItemsToMenu.menu, &s_addItemsToMenu.back );
}

/*
=================
BOTS_AddItemsToMenu_f
=================
*/
void BOTS_AddItemsToMenu_f( BOTS_ItemAction_e menuAction_ev ) 
{
	// force as top level menu
	uis.menusp = 0;  

	// set menu cursor to a nice location
	uis.cursorx = 319;
	uis.cursory = 80;

	globalItemAction_ev = menuAction_ev;

	// setup the menu structure
	BOTS_AddItemsToMenu_Init();

	// show the menus
	UI_PushMenu( &s_addItemsToMenu.menu );
}
